package com.jiangyg.mall.coupon.scheduled;

import com.jiangyg.mall.core.utils.Logger;
import com.jiangyg.mall.coupon.service.HomeAdvService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * 类描述：首页广告点击次数定时任务
 *
 * @author jiangyg
 * @date 2022-11-26
 */
@Slf4j
@Component
@EnableScheduling
public class HomeAdvClickCountScheduled {

    private final HomeAdvService homeAdvService;

    @Autowired
    public HomeAdvClickCountScheduled(HomeAdvService homeAdvService) {
        this.homeAdvService = homeAdvService;
    }

    /**
     * 功能描述：每15分钟，把 redis 中广告的点击次数同步到数据库中
     */
    @Scheduled(cron = "0 0/15 * * * ?")
    public void execute1() {
        // TODO 切换分布式定时任务框架
        Logger.info(log, () -> "[定时任务]-首页广告点击次数持久化开始执行……");
        homeAdvService.clickCountDataPersistent();
        Logger.info(log, () -> "[定时任务]-首页广告点击次数持久化执行完毕……");
    }

    /**
     * 功能描述：每天晚上23点清除无效的广告点击缓存
     */
    @Scheduled(cron = "0 0 23 * * ?")
    public void execute2() {
        // TODO 可以考虑追加定时任务配置，手动开启关闭任务，做系统降级处理
        // TODO 切换分布式定时任务框架
        Logger.info(log, () -> "[定时任务]-清除无效的广告点击缓存开始执行……");
        homeAdvService.cleanInvalidClickCountCache();
        Logger.info(log, () -> "[定时任务]-清除无效的广告点击缓存执行完毕……");
    }

}
